package com.android.weather1;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RSSHandler extends DefaultHandler {
    private WeatherYahoo wYahoo;
    private StringBuilder strText;
 
    public WeatherYahoo getWeather() {
      return this.wYahoo;
    }
    
    @Override
    public void startDocument() throws SAXException {
 
        super.startDocument();  
        strText = new StringBuilder();
    }
 
    @Override
    public void endDocument() throws SAXException {
       // nothing we need to do here
    }
    
    @Override
    public void characters(char[] ch, int start, int length)
                   throws SAXException {
 
        super.characters(ch, start, length);
 
        if (this.wYahoo != null)
        	strText.append(ch, start, length);
    }
 
    @Override
    public void startElement(String uri, String localName,
                   String name, Attributes attributes) throws SAXException {
 
        super.startElement(uri, localName, name, attributes);
 
        if (localName.equals("channel")) {
            wYahoo  = new WeatherYahoo();
        }
        else if (localName.equals("location")) {
        	wYahoo.City    = attributes.getValue(wYahoo.cityAttrIDX);
        	wYahoo.Region  = attributes.getValue(wYahoo.regionAttrIDX);
        	wYahoo.Country = attributes.getValue(wYahoo.countryAttrIDX);
        }
        else if (localName.equals("condition")) {
        	wYahoo.textWeather = attributes.getValue(wYahoo.textWAttrIDX);
        	wYahoo.Temp        = attributes.getValue(wYahoo.TempAttrIDX);
        }
                
    }

    @Override
    public void endElement(String uri, String localName, String name)
                   throws SAXException {
 
        super.endElement(uri, localName, name);
 
        if (this.wYahoo != null) {
 
            if (localName.equals("lastBuildDate"))
            	wYahoo.lastBuildDate = strText.toString();
            
           strText.setLength(0);
        }

    }
 
}
